import { defineStore } from "pinia";
import api from '../api/index'
export const useStore = defineStore("store", {
    state: () => {
        return {
            isLogin: null,
            info: null,
            storeList: null
        }
    },
    actions: {
        async changeIsLoginAsync() {
            let bool = false;
            let { code } = await api.checkLogin();
            if (+code === 0) bool = true;
            this.isLogin = bool
        },
        changeIsLogin(bool) {
            this.isLogin = bool
        },
        async changeInfoAsync() {
            let { code, data } = await api.userInfo();
            if (+code === 0) {
                this.info = data
            }
        },
        changeInfo(data) {
            this.info = data
        },
        async changeStoreListAsync() {
            let { code, data } = await api.storeList();
            if (+code !== 0) data = [];
            this.storeList = data
        },
        changeStoreList(payload) {
            this.storeList = payload
        },
        removeStoreList(id) {
            if (this.storeList === null) return;
            this.storeList = this.storeList.filter(item => {
                return +item.id !== +id;
            });
        }
    },
    persist: true
});